package ntu.edu.cn.action;

import com.opensymphony.xwork2.ActionSupport;
import ntu.edu.cn.http.HttpPoolManager;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0
 * Created by Morven Qiu on 8/9/2016.
 * this action provide the basic method for other action class
 */
public class BaseAction extends ActionSupport{


    private static final Logger logger = Logger.getLogger(BaseAction.class);
    private static final int CONNECTION_TIMEOUT = 2*1000;
    private static final int WAIT_TIMEOUT = 2*1000;
    private static final int CONN_SOCKET_TIMEOUT = 2*1000;

    public void configParam(HttpRequestBase httpRequestBase) {
        httpRequestBase.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
        httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        httpRequestBase.setHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
        httpRequestBase.setHeader("Accept-Encoding", "gzip,deflate");
        httpRequestBase.setHeader("Pragma", "no-cache");
        httpRequestBase.setHeader("Content-Length", "0");
        // 配置请求的超时设置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(WAIT_TIMEOUT)
                .setConnectTimeout(CONNECTION_TIMEOUT)
                .setSocketTimeout(CONN_SOCKET_TIMEOUT)
                .build();
        httpRequestBase.setConfig(requestConfig);
    }

    public HttpClientBuilder getHttpClient(){
        return  HttpPoolManager.getHttpClientConnection();
    }

    protected HttpServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

    protected HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    protected HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * get a useful httpclient which already has a cookiestore
     * @return
     */
    protected CloseableHttpClient getClosableHttpClient(){
        return getHttpClient().setDefaultCookieStore((BasicCookieStore) getSession().getAttribute("basicCookieStore")).build();
    }

    protected PrintWriter getWriter() {
        try {
            return getResponse().getWriter();
        }catch(IOException e){
            logger.error("error to get response");
        }
       return null;
    }

    protected void releaseConnection(CloseableHttpClient httpClient){

    }

}
